package modelTest;

import static org.junit.Assert.assertEquals;

import java.util.Calendar;
import java.util.GregorianCalendar;

import model.Auction;
import model.Control;
import model.NPO;

import org.junit.Test;

/**
 * Tests the Control class
 * 
 * @author Major Bruno
 */
public class ControlTest {

	// A Control object for testing
	private static Control ctrl = new Control();

	//some Calendar objects for testing
	private Calendar one = new GregorianCalendar();

	private Calendar two = new GregorianCalendar();

	private Calendar three = new GregorianCalendar();
	
	@Test
	public void testAddAuction() {
		NPO npo1 = new NPO("NPO1");
		NPO npo2 = new NPO("NPO2");
		NPO npo3 = new NPO("NPO3");
		NPO npo4 = new NPO("NPO4");
		NPO npo5 = new NPO("NPO5");
		NPO npo6 = new NPO("NPO6");
		NPO npo7 = new NPO("NPO7");
		NPO npo8 = new NPO("NPO8");
		NPO npo9 = new NPO("NPO9");
		NPO npo10 = new NPO("NPO10");
		NPO npo11 = new NPO("NPO11");
		NPO npo12 = new NPO("NPO12");
		NPO npo13 = new NPO("NPO13");
		NPO npo14 = new NPO("NPO14");
		NPO npo15 = new NPO("NPO15");
		NPO npo16 = new NPO("NPO16");
		NPO npo17 = new NPO("NPO17");
		NPO npo18 = new NPO("NPO18");
		NPO npo19 = new NPO("NPO19");
		NPO npo20 = new NPO("NPO20");
		NPO npo21 = new NPO("NPO21");
		NPO npo22 = new NPO("NPO22");
		NPO npo23 = new NPO("NPO23");
		NPO npo24 = new NPO("NPO24");
		NPO npo25 = new NPO("NPO25");
		NPO npo26 = new NPO("NPO26");
		
		ctrl.newUser("staff1", 3, npo25, 1);
		ctrl.newUser("staff2", 3, npo1, 2);
		ctrl.newUser("staff3", 3, npo26, 3);
		ctrl.login("staff2");
		
		Calendar cal1 = new GregorianCalendar();
		cal1.set(2012, 5, 31, 16, 0);
		Calendar cal2 = new GregorianCalendar();
		cal2.set(2012, 6, 1, 10, 0);
		Calendar cal3 = new GregorianCalendar();
		cal3.set(2012, 6, 1, 14, 0);
		Calendar cal4 = new GregorianCalendar();
		cal4.set(2012, 6, 6, 13, 0);
		Calendar cal5 = new GregorianCalendar();
		cal5.set(2012, 6, 7, 18, 30);
		Calendar cal6 = new GregorianCalendar();
		cal6.set(2012, 6, 8, 12, 0);
		Calendar cal7 = new GregorianCalendar();
		cal7.set(2012, 6, 11, 16, 0);
		Calendar cal8 = new GregorianCalendar();
		cal8.set(2012, 6, 12, 7, 30);
		Calendar cal9 = new GregorianCalendar();
		cal9.set(2012, 6, 13, 11, 0);
		Calendar cal10 = new GregorianCalendar();
		cal10.set(2012, 6, 14, 7, 0);
		Calendar cal11 = new GregorianCalendar();
		cal11.set(2012, 6, 15, 13, 30);
		Calendar cal12 = new GregorianCalendar();
		cal12.set(2012, 6, 18, 15, 0);
		Calendar cal13 = new GregorianCalendar();
		cal13.set(2012, 6, 19, 17, 0);
		Calendar cal14 = new GregorianCalendar();
		cal14.set(2012, 6, 20, 12, 0);
		Calendar cal15 = new GregorianCalendar();
		cal15.set(2012, 6, 21, 10, 0);
		Calendar cal16 = new GregorianCalendar();
		cal16.set(2012, 6, 22, 7, 0);
		Calendar cal17 = new GregorianCalendar();
		cal17.set(2012, 6, 25, 15, 0);
		Calendar cal18 = new GregorianCalendar();
		cal18.set(2012, 6, 26, 12, 0);
		Calendar cal19 = new GregorianCalendar();
		cal19.set(2012, 6, 27, 16, 0);
		Calendar cal20 = new GregorianCalendar();
		cal20.set(2012, 6, 28, 15, 0);
		Calendar cal21 = new GregorianCalendar();
		cal21.set(2012, 6, 29, 12, 0);
		Calendar cal22 = new GregorianCalendar();
		cal22.set(2012, 7, 2, 14, 0);
		Calendar cal23 = new GregorianCalendar();
		cal23.set(2012, 7, 3, 15, 0);
		Calendar cal24 = new GregorianCalendar();
		cal24.set(2012, 7, 4, 10, 0);
		
		Calendar cal25 = new GregorianCalendar();
		cal25.set(2012, 10, 31, 10, 0);
		Calendar cal26 = new GregorianCalendar();
		cal26.set(2012, 6, 16, 10, 0);
		Calendar cal27 = new GregorianCalendar();
		cal27.set(2012, 5, 31, 19, 0);
		Calendar cal28 = new GregorianCalendar();
		cal28.set(2012, 6, 1, 19, 0);
		Calendar cal29 = new GregorianCalendar();
		cal29.set(2012, 7, 15, 19, 0);
		Calendar cal30 = new GregorianCalendar();
		cal30.set(2012, 6, 2, 19, 0);
		Calendar cal31 = new GregorianCalendar();
		cal31.set(2012, 7, 5, 19, 0);
		
		//add some good ones
		ctrl.auctions.add(new Auction(npo1, cal1, 4));
		ctrl.auctions.add(new Auction(npo2, cal2, 3));
		ctrl.auctions.add(new Auction(npo3, cal3, 2));
		ctrl.auctions.add(new Auction(npo4, cal4, 5));
		ctrl.auctions.add(new Auction(npo5, cal5, 7));
		ctrl.auctions.add(new Auction(npo6, cal6, 3));
		ctrl.auctions.add(new Auction(npo7, cal7, 5));
		ctrl.auctions.add(new Auction(npo8, cal8, 7));
		ctrl.auctions.add(new Auction(npo9, cal9, 3));
		ctrl.auctions.add(new Auction(npo10, cal10, 5));
		ctrl.auctions.add(new Auction(npo11, cal11, 7));
		ctrl.auctions.add(new Auction(npo12, cal12, 3));
		ctrl.auctions.add(new Auction(npo13, cal13, 5));
		ctrl.auctions.add(new Auction(npo14, cal14, 7));
		ctrl.auctions.add(new Auction(npo15, cal15, 3));
		ctrl.auctions.add(new Auction(npo16, cal16, 5));
		ctrl.auctions.add(new Auction(npo17, cal17, 7));
		ctrl.auctions.add(new Auction(npo18, cal18, 3));
		ctrl.auctions.add(new Auction(npo19, cal19, 5));
		ctrl.auctions.add(new Auction(npo20, cal20, 7));
		ctrl.auctions.add(new Auction(npo21, cal21, 3));
		ctrl.auctions.add(new Auction(npo22, cal22, 5));
		ctrl.auctions.add(new Auction(npo23, cal23, 7));
		ctrl.auctions.add(new Auction(npo24, cal24, 3));
		
		//add one too far in advance
		assertEquals(Control.FAIL6, ctrl.addAuction(cal29, 5));
		
		ctrl.login("staff1");
		
		assertEquals(Control.FAIL2, ctrl.addAuction(cal25, 5));
		assertEquals(Control.FAIL3, ctrl.addAuction(cal26, 5));
		assertEquals(Control.FAIL4, ctrl.addAuction(cal28, 5));	
		assertEquals(Control.PASS, ctrl.addAuction(cal30, 5));
		
		ctrl.login("staff3");
		
		assertEquals(Control.FAIL1, ctrl.addAuction(cal31, 5));
	}

	/*
	 * NOTE: The body of tests below test the loading and saving of the file system for the program.
	 * These tests are commented out because they will not work unless the default set of files are loaded.
	 * If you wish to see these tests run, simply make sure that the default file set is being loaded.
	 */
	
//	@Before
//	public void initialize() {
//		// set up some Dates
//		DateFormat my_format = new SimpleDateFormat("MM.dd.yyyy");
//		one.set(2012, 5, 23, 2, 30);
//		two.set(2012, 6, 5, 5, 0);
//		three.set(2012, 7, 11, 2, 15);
//	}
//	
//	@Test
//	public void testLoadNPOs() {
//		ctrl.loadNPOs();
//
//		assertEquals("Holodrum", (ctrl.npos.get("Holodrum")).getName());
//		assertEquals("Hyrule", (ctrl.npos.get("Hyrule")).getName());
//		assertEquals("Termina", (ctrl.npos.get("Termina")).getName());
//	}
//
//	@Test
//	public void testLoadUsers() {
//		ctrl.loadUsers();
//
//		assertEquals("Link", (ctrl.users.get(0)).getName());
//		assertEquals("Zelda", (ctrl.users.get(1)).getName());
//		assertEquals("Holodrum", (((NPOStaff) ctrl.users.get(2))).getNPO()
//				.getName());
//	}
//
//	@SuppressWarnings("deprecation")
//	@Test
//	public void testLoadAuctions() {
//		ctrl.loadAuctions();
//
//		assertEquals("Hyrule May 23, 2012", (ctrl.auctions.get(0)).getName());
//		assertEquals(Double.doubleToLongBits(5.0),
//				Double.doubleToLongBits((ctrl.auctions.get(0)).getDuration()));
//
//		assertEquals("Termina Jun 5, 2012", (ctrl.auctions.get(1)).getName());
//		assertEquals(Double.doubleToLongBits(2.35),
//				Double.doubleToLongBits((ctrl.auctions.get(1)).getDuration()));
//		
//		assertEquals("Holodrum Jul 11, 2012", (ctrl.auctions.get(2)).getName());
//		assertEquals(Double.doubleToLongBits(7.75),
//				Double.doubleToLongBits((ctrl.auctions.get(2)).getDuration()));
//	}
//
//	@Test
//	public void testSaveUsers() {
//
//		Bidder four = new Bidder("Malon");
//
//		Item i1 = new Item("Ocarina", 200,
//				"A fine piece engraved with the mark of the royal family.");
//
//		four.newBid(i1, 200);
//
//		ctrl.newUser("Malon", 2, null, 0);
//
//		ctrl.saveUsers();
//	}
//
//	@Test
//	public void testSaveNPOs() {
//		NPO one = new NPO("GoronVillage");
//		NPO two = new NPO("ZorasDomain");
//		NPO three = new NPO("TempleOfTime");
//
//		ctrl.npos.put("GoronVillage", one);
//		ctrl.npos.put("ZorasDomain", two);
//		ctrl.npos.put("TempleOfTime", three);
//
//		ctrl.saveNPOs();
//	}
//
//	@Test
//	public void testSaveAuctions() {
//
//		NPO npo1 = new NPO("ClockTown");
//		Calendar cal1 = new GregorianCalendar();
//		cal1.set(2012, 6, 10, 5, 30);
//
//		Auction one = new Auction(npo1, cal1, 5);
//
//		Item i1 = new Item("Ocarina", 125.25,
//				"A fine piece engraved with the mark of the royal family.");
//		Item i2 = new Item("Boomerang", 30.50,
//				"Retrieved from the belly of Lord Jabu Jabu");
//		Item i3 = new Item("Hero'sBow", 50.00, "Found in the Forest Temple.");
//
//		one.addItem(i1);
//		one.addItem(i2);
//		one.addItem(i3);
//
//		ctrl.auctions.add(one);
//
//		ctrl.saveAuctions();
//	}
}
